package mytest.wd2;

import com.alibaba.fastjson.JSONObject;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.*;

/**
 * 71b32d46-81f6-4216-a4cf-3457591bd6dc
 */
public class Test1 {

    public static void main(String[] args) {


       /*
        用户 ： 71b32d46-81f6-4216-a4cf-3457591bd6dc
        数仓   ： "WhatsApp Business"
        hbase : "WhatsApp Business";
        */


        //hbase
        String path1 = "C:\\Users\\86187\\Desktop\\71b32d46-81f6-4216-a4cf-3457591bd6dc.txt";
        List<String> list1 = readFile(path1);
        String s1 = list1.get(0);
        Set<String> setOne = getSetOne(s1);
        System.out.println("hbase：" + setOne.size());
        System.out.println("---------------------------------------");

        //数仓
        String path2 = "G:\\Microsoft downloads\\71b32d46-81f6-4216-a4cf-3457591bd6dc.csv";
        List<String> list2 = readFile(path2);
        Set<String> setTwo = getSetTwo(list2);
        System.out.println("数仓：" + setTwo.size());

        System.out.println("---------------------------------------");
        compareSet(setOne, setTwo);
    }

    public static Set<String> getSetOne(String s2) {
        Map<String, Object> map = JSONObject.parseObject(s2);

        Set<String> keys = map.keySet();

        HashMap<String, Set<String>> hashMap = new HashMap<>();

        for (String key : keys) {
            String time = key.substring(4).substring(0,13);
            String values = (String) map.get(key);
            Map<String, Object> map1 = JSONObject.parseObject(values);
            String name = (String) map1.get("appName");
            Set<String> strings = hashMap.computeIfAbsent(time, k -> new HashSet<>());
            strings.add(name);
        }

        for (Map.Entry<String, Set<String>> entry : hashMap.entrySet()) {
            System.out.println(entry.getKey() + " : " + entry.getValue().size());
        }

        Set<String> set1 = new HashSet<>();

        for (Map.Entry<String, Set<String>> entry : hashMap.entrySet()) {
            Set<String> value = entry.getValue();
            for (String s : value) {
                set1.add(s);
            }
        }
        return set1;
    }

    public static Set<String> getSetTwo(List<String> lists) {

        HashMap<String, Set<String>> hashMap2 = new HashMap<>();

        for (String list : lists) {
            Map<String, Object> map2 = JSONObject.parseObject(list);
            String sessionGid = (String) map2.get("session_gid");
            String appName = (String) map2.get("appName");
            hashMap2.computeIfAbsent(sessionGid, l -> new HashSet<>()).add(appName);
        }

        for (Map.Entry<String, Set<String>> entry : hashMap2.entrySet()) {
            System.out.println(entry.getKey() + ":" + entry.getValue().size());
        }

        Set<String> set2 = new HashSet<>();

        for (Map.Entry<String, Set<String>> entry : hashMap2.entrySet()) {
            Set<String> value = entry.getValue();
            for (String s : value) {
                set2.add(s);
            }
        }
        return set2;
    }

    public static void compareSet(Set<String> set1, Set<String> set2) {
        int temp = 0;
        if (set1.size() >= set2.size()) {
            for (String s : set1) {
                if (!set2.contains(s)) {
                    System.out.println(s);
                    temp ++;
                }
            }
        } else {
            for (String s : set2) {
                if (!set1.contains(s)) {
                    System.out.println(s);
                    temp ++;
                }
            }
        }
        System.out.println(temp);
    }

    private static List<String> readFile(String path) {
        List<String> lines = new ArrayList<>();
        FileReader fr = null;
        try {
            fr = new FileReader(path);
            BufferedReader br = new BufferedReader(fr);
            String line = "";
            String[] arrs = null;
            while ((line = br.readLine()) != null) {
                lines.add(line);
            }
            br.close();
            fr.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return lines;
    }
}
